package com.tyl.lock;

import java.util.concurrent.atomic.AtomicReference;

public class SpinLockDemo {

    //int 0
    //Thread null
    AtomicReference<Object> atomicReference = new AtomicReference<>();
    //加锁
    public  void myLock(){
        Thread thread=Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"myLock");
        while (!atomicReference.compareAndSet(null, thread)) {

        }
    }

    //解锁
    public void myUnLock(){
        Thread thread=Thread.currentThread();
        System.out.println(Thread.currentThread().getName()+"myUnLock");
        atomicReference.compareAndSet(thread,null);
    }
}
